<?php

namespace App\Providers;

use App\Models\GroupUser;
use App\Models\User;
use App\Observers\UserObserver;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
use App\Models\Bet;
use App\Models\Quiz;
use App\Models\Group;
use App\Observers\BetObserver;
use App\Observers\QuizObserver;
use App\Observers\GroupObserver;
use App\Repositories\QuizRepository;
use App\Repositories\GroupRepository;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191);
        Group::observe(GroupObserver::class);
        Quiz::observe(QuizObserver::class);
        Bet::observe(BetObserver::class);
        User::observe(UserObserver::class);

        Route::bind('group', function ($id) {
            return Group::findCache($id, function ($query) {
                $query->with('creater');
            });
        });

        Route::bind('quiz', function ($id) {
            return Quiz::findCache($id, function ($query) {
                $query->with(['creater', 'statement']);
            });
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton(GroupRepository::class, function ($app) {
            return new GroupRepository;
        });
        $this->app->singleton(QuizRepository::class, function ($app) {
            return new QuizRepository;
        });
    }
}
